﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Windows.Forms;

namespace BLL
{
    public class GetPassportInfoFormServer
    {
        public static System.Net.CookieContainer cookie = new System.Net.CookieContainer();
        public string TaskCode;
        public GetPassportInfoFormServer(string taskCode)
        {            
            TaskCode = taskCode;
        }
        public Common.PassportInfoData GetPassportInfo()
        {
            BLL.CardIssueManage.CardIssueManage cim = new BLL.CardIssueManage.CardIssueManage();
            cim.CookieContainer = cookie;
            BLL.CardIssueManage.IcmCardInfoReturn carinfo;
            WFNetLib.Log.SystemLogData l = new WFNetLib.Log.SystemLogData();
            try
            {
                carinfo = cim.getIcmCardInfo(null, TaskCode);               
            }
            catch (Exception ex)
            {
                //insert log  
                l.LogRemark = "无法连接";
                l.LogType = Common.LogType.网络服务.ToString();
                l.LogContent = Properties.Settings.Default.BLL_CardIssueManage_CardIssueManage + ":" + ex.Message;                
                l.InserSQLLog();
                MessageBox.Show(ex.Message);
                return null;
            }
            if(carinfo.returnCode!="00000")
            {               
                l.LogRemark = "查询失败";
                l.LogType = Common.LogType.网络服务.ToString();
                l.LogContent = "条码为:" + TaskCode + carinfo.returnCodeDesc;
                l.InserSQLLog();
                MessageBox.Show("该条码信息未知");
                return null;
            }
            Common.PassportInfoData p = new Common.PassportInfoData();
            p.Birthday = DateTime.ParseExact(carinfo.icmCardInfo.birthday, "yyyyMMdd", null);
            p.CardCode = carinfo.icmCardInfo.cardCode;
            p.CardID = carinfo.icmCardInfo.idCard;
            p.Name = carinfo.icmCardInfo.chFullName;
            p.OprationNumber = carinfo.icmCardInfo.taskCode;
//             if(p==null)
//             {               
//                 l.LogRemark = "格式有误";
//                 l.LogType = Common.LogType.网络服务.ToString();
//                 l.LogContent = "从服务器获得条形码为" + TaskCode+ "的护照信息数据有误:" + info; 
//                 l.InserSQLLog();
//                 return null;               
//             }
            p.TaskCode = TaskCode;
            p.Operator = Common.AppInfo.Operator;
            p.AddTime = DateTime.Now;
            return p;
        }
        public bool CheckIcmApply(String OprationNumber)
        {
            BLL.CardIssueManage.CardIssueManage cim = new BLL.CardIssueManage.CardIssueManage();
            cim.CookieContainer = cookie;
            BLL.CardIssueManage.CheckIcmReturn cir;
            WFNetLib.Log.SystemLogData l = new WFNetLib.Log.SystemLogData();
            StringBuilder sb = new StringBuilder();
            try
            {
                cir = cim.checkIcmApply(OprationNumber);
                sb.Append(cir.returnCode + ";");
                sb.Append(cir.returnCodeDesc + ";");
                for(int i=0;i<7;i++)
                {
                    sb.Append(cir.icmCheckResults[i]);
                }
            }
            catch (Exception ex)
            {
                //insert log  
                l.LogRemark = "无法连接";
                l.LogType = Common.LogType.网络服务.ToString();
                l.LogContent = Properties.Settings.Default.BLL_CardIssueManage_CardIssueManage + ":" + ex.Message;
                l.InserSQLLog();
                MessageBox.Show(ex.Message);
                return false;
            }
            l.LogRemark = "核查结果";
            l.LogType = Common.LogType.网络服务.ToString();
            l.LogContent = "业务编码为:" + OprationNumber + "  :" + sb.ToString();
            l.InserSQLLog();
            if (cir.returnCode == "00000")
            {
                for (int i = 0; i < 7; i++)
                {
                    if (cir.icmCheckResults[i] == "1")
                    {
                        //MessageShow(i);
                        MessageBox.Show("警报：请在出入境系统中完成此操作");
                        return false;
                    }
                }
                MessageBox.Show("信息核查正常，可以发证!!!");
                return true;
            }
            else if (cir.returnCode == "02000")
            {
                for (int i = 0; i < 7; i++)
                {
                    if (cir.icmCheckResults[i] == "1")
                    {
                        MessageShow(i);
                        //return false;
                    }
                }
//                 if (WFNetLib.WFGlobal.MessageYseOrNo("是否进行发证","出现需要核查信息") == DialogResult.OK)
//                 {
//                     return true;
//                 }
                MessageBox.Show("警报：请在出入境系统中完成此操作");
                return false;
            }
            else
            {
                MessageBox.Show(cir.returnCodeDesc);
                return false;
            }
        }   
        private void MessageShow(int index)
        {
            switch(index)
            {
                case 0:
                    MessageBox.Show("警报：重复申请！！");
                    break;
                case 1:
                    MessageBox.Show("警报：在逃人员！！");
                    break;
                case 2:
                    MessageBox.Show("警报：内控人员！！");
                    break;
                case 3:
                    MessageBox.Show("警报：不准出境人员！！");
                    break;
                case 4:
                    MessageBox.Show("警报：违法违规人员！！");
                    break;
                case 5:
                    MessageBox.Show("警报：国家工作人员！！");
                    break;
                case 6:
                    MessageBox.Show("警报：涉案人员！！");
                    break;
            }
        }
        public bool doCardIssue(String OprationNumber)
        {
            BLL.CardIssueManage.CardIssueManage cim = new BLL.CardIssueManage.CardIssueManage();
            cim.CookieContainer = cookie;
            BLL.CardIssueManage.OperationReturn cir;
            WFNetLib.Log.SystemLogData l = new WFNetLib.Log.SystemLogData();            
            try
            {
                cir = cim.doCardIssue(OprationNumber);                
            }
            catch (Exception ex)
            {
                //insert log  
                l.LogRemark = "无法连接";
                l.LogType = Common.LogType.网络服务.ToString();
                l.LogContent = Properties.Settings.Default.BLL_CardIssueManage_CardIssueManage + ":" + ex.Message;
                l.InserSQLLog();
                MessageBox.Show(ex.Message);
                return false;
            }            
            if (cir.returnCode != "00000")
            {
                l.LogRemark = "发证失败";
                l.LogType = Common.LogType.网络服务.ToString();
                l.LogContent = "业务编码为:" + OprationNumber + "  :" + cir.returnCodeDesc;
                l.InserSQLLog();
                MessageBox.Show("发证失败,业务编码为:" + OprationNumber + "  :" + cir.returnCodeDesc);
                return false;
            }
            return true;            
        }
    }
}
